package com.wisewin.pose.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wisewin.pose.system.Constants;

public class WxUtil {

    private static final String appId = "wx3ea7751c5c92c5b4";
    private static final String appSecret = "d892d1d13c04a37fcaaa13d6ca3fdf2c";

    private static final String appIdv2 = "wxdb4b1d6346831652";
    private static final String appSecretv2 = "d27b6dd19aaee96aa777bf4e7ceba346";

    // 获取accessToken
    public static String getAccessToken(String mark) {

        String currAppId=WxUtil.getAppId(mark);
        String currAppSecret=WxUtil.getAppSecret(mark);

        // 从redis中获取accessToken
        String accessToken = JedisUtil.get("accessToken" + currAppId);

        if (accessToken == null) {

            // 获取token
            String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", currAppId, currAppSecret);
            JSONObject token = JSON.parseObject(HttpUtil.get(token_url));
            if (token == null) {
                return null;
            }
            accessToken = token.getString("access_token");

            // 存入redis
            JedisUtil.setEx("accessToken" + currAppId, accessToken, Constants.ONE_HOUR_LONG);
        }

        return accessToken;
    }

    // 获取密钥
    public static String getAppSecret(String mark) {
        if (mark==null||mark.equals("")||mark.equals("sdyy")) {
            return appSecret;
        } else {
            return appSecretv2;
        }
    }

    // 获取appId
    public static String getAppId(String mark) {
        if (mark==null||mark.equals("")||mark.equals("sdyy")) {
            return appId;
        } else {
            return appIdv2;
        }
    }

}
